package com.example.javawebserver.demos.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.javawebserver.demos.web.mapper.ProfitabilityAnalysisMapper;
import com.example.javawebserver.demos.web.model.FundModel;
import com.example.javawebserver.demos.web.service.ProfitabilityAnalysisService;
import com.example.javawebserver.demos.web.utils.JsonData;
import com.example.javawebserver.demos.web.vo.SelectVo;
import com.example.javawebserver.pojo.DeletePojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ProfitabilityAnalysisServiceImpl implements ProfitabilityAnalysisService {

    @Autowired
    private ProfitabilityAnalysisMapper profitabilityAnalysisMapper;


    @Override
    public JsonData select(int id) {
        if(id == 0){
            return JsonData.buildError("前端传入的id值为空");
        }
        SelectVo resultVo = profitabilityAnalysisMapper.select(id);
        return JsonData.buildSuccess(resultVo);
    }

    @Override
    public JsonData insert(FundModel fundModel) {
        if(ObjectUtils.isEmpty(fundModel)){
            return JsonData.buildError("入参实体为空！！");
        }
        int insertNum = profitabilityAnalysisMapper.insert(fundModel);
        return JsonData.buildSuccess("成功新增条数：" + insertNum);
    }

    @Override
    public JsonData update(FundModel fundModel, int id) {
        if(ObjectUtils.isEmpty(fundModel)){
            return JsonData.buildError("传入的修改实体为空!!");
        }
        if(id == 0){
            return JsonData.buildError("传入的id为空!!");
        }
        UpdateWrapper<FundModel> UpdateWrapper = new UpdateWrapper<FundModel>();
        UpdateWrapper.set("SPU",fundModel.getSpu())
                .set("CNY",fundModel.getCny())
                .eq("id",fundModel.getId());
        int update = profitabilityAnalysisMapper.update(fundModel, UpdateWrapper);
        return JsonData.buildSuccess("成功更新了" + update);
    }

    @Override
    public JsonData delete(DeletePojo deletePojo) {
        if(deletePojo.getId() == 0){
            return JsonData.buildError("传入的入参id为空！！");
        }
        int deleteResult = profitabilityAnalysisMapper.deleteById(deletePojo.getId());
        return JsonData.buildSuccess("删除了" + deleteResult + "行");
    }

    @Override
    public JsonData selectAll() {
        List<SelectVo> selectVoList = profitabilityAnalysisMapper.selectAll();
        return JsonData.buildSuccess(selectVoList);
    }


}
